﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KgUtils.Web
{
    public partial class CacheManager
    {
        protected static Dictionary<string, object> cacheValues = new Dictionary<string, object>();
        private object SyncRoot = new object();

        public void SetCache(string key, object value)
        {
            lock (SyncRoot)
            {
                cacheValues[key] = value;
            }
        }

        public bool IsDefined(string key)
        {
            return cacheValues.ContainsKey(key);
        }

        public T GetCache<T>(string key)
        {
            if (cacheValues.ContainsKey(key))
                return (T)cacheValues[key];
            else
                return default(T);
        }

        public void ClearCache(string key)
        {
            lock (SyncRoot)
            {
                cacheValues.Remove(key);
            }
        }

        public void ClearCache()
        {
            lock (SyncRoot)
            {
                cacheValues.Clear();
            }
        }

        #region Singleton
        private static readonly CacheManager instance = new CacheManager();
        public static CacheManager Instance
        {
            get { return instance; }
        }

        private CacheManager()
        {
        }
        #endregion
    }
}
